<template>
	<view class="content">
		<view class="bg"></view>
		<view class="list">
			<view class="item" v-for="(item, index) in addressList.list" :key="index" @click="checkAddress(item)">
				<view class="u-box">
					<text class="name">{{item.realname}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>
				<view class="address-box">
					<text v-if="item.isdefault==1" class="tag">默认</text>
					<text class="address">{{item.province}} {{item.city}} {{item.area}} {{item.address}}</text>
					<image src="/static/bianji.png" mode="widthFix" @click.stop="addAddress('edit', item)"></image>
				</view>
				<view class="edit">
					<view class="flex_box" @click.stop="setMoren(item)">
						<uni-icons v-if="item.isdefault==1" type="checkbox-filled" size="16" color="#8615E1"></uni-icons>
						<uni-icons v-else type="circle" size="16" color="#DEDEDE"></uni-icons>
						 <text>默认地址</text>
					</view>
					<view class="flex_box" @click.stop="delAdd(item.id)">
						<uni-icons type="trash" size="16" color="#BBC3C9"></uni-icons><text>删除</text>
					</view>
				</view>
			</view>
		</view>
		
		<button class="add-btn" @click="addAddress('add')">+ 新增地址</button>
	</view>
</template>

<script>
	import {
		bus
	} from '../../static/js/bus.js';
	export default {
		data() {
			return {
				source: 0,
				addressList: [],
				page: 1,
				cartids: '',
				type: '',
				id: '',
				optionid: '',
				total: '',
				zhiboid: '',
				options: '',
				isVip: 0,
				isGroupBuy: 0,
			}
		},
		onLoad(option) {
			this.zhiboid = option.zhiboid;
			this.zhiboid = this.zhiboid == undefined ? '' : this.zhiboid;
			if (option.source) {
				this.source = option.source;
			}
			this.options = option;
			if (option.source) {
				this.isVip = option.isVip;
			}
		},
		onBackPress(e) {
			if(e.from=='backbutton'&&!this.type){
				uni.navigateBack({
					delta:2
				})
				return true;
			}
		},
		onShow() {
			var that = this;
			this.page = 1;
			if (that.options.type == '1') {
				that.cartids = that.options.cartids;
				that.type = that.options.type;
			} else if (that.options.type == '2') {
				that.type = that.options.type;
				that.id = that.options.id;
				that.optionid = that.options.optionid;
				that.total = that.options.total;
				that.isVip = that.options.isVip;
				that.isGroupBuy = that.options.isGroupBuy;
			}
			if (that.options.type == '3') {
				that.type = that.options.type;
				that.id = that.options.id;
				that.shopid = that.options.shopid;
			}
			this.$axios('address/display', "POST", 'shop', {
				page: that.page
			}).then(res => {
				if (res.data.code == 200) {
					that.addressList = res.data.data
				}
			}, (error) => {

			})
		},
		onReachBottom() {
			var that = this;
			this.page = this.page + 1;
			this.$axios('address/display', "POST", 'shop', {
				page: that.page
			}).then(res => {
				if (res.data.code == 200) {
					that.addressList.list = [...that.addressList.list,...res.data.data.list]
				}
			}, (error) => {

			})
		},
		methods: {
			delAdd(id) {
				uni.showModal({
					title: '提示',
					content: '确定删除？',
					success: res=> {
						if (res.confirm) {
							this.$axios('address/remove', "POST", 'shop', {
								id: id
							}).then(res => {
								if (res.data.code == 200) {
									this.$api.msg('删除成功');
									this.$axios('address/display', "POST", 'shop', {
										page: 1
									}).then(res => {
										if (res.data.code == 200) {
											this.addressList = res.data.data
										}
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			setMoren(data){
				this.$axios('address/submit', "POST", 'shop', {
					realname: data.realname,
					mobile: data.mobile,
					province: data.province,
					city: data.city,
					area: data.area,
					address: data.address,
					isdefault: 1,
					id: data.id
				}).then(res => {
					if (res.data.code == 200) {
						this.$axios('address/display', "POST", 'shop', {
							page: 1
						}).then(res => {
							if (res.data.code == 200) {
								this.addressList = res.data.data
							}
						})
					}
				})
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					// this.$api.prePage().info.address = item;
					// bus.$emit('addre',item);
					uni.setStorageSync('addre', JSON.stringify(item));
					// uni.navigateBack();
					uni.navigateTo({
						url: '/pages/order/createOrder?type=' + this.type + '&cartids=' + this.cartids + '&zhiboid=' + this.zhiboid
					})
				} else if (this.source == 2) {
					// this.$api.prePage().addressData = item;
					uni.setStorageSync('addre', JSON.stringify(item));
					uni.navigateTo({
						url: '/pages/order/createOrder?type=' + this.type + '&id=' + this.id + '&optionid=' + this.optionid + '&total=' +
							this.total + '&source=2&zhiboid=' + this.zhiboid+'&isVip='+this.isVip+'&isGroupBuy='+this.isGroupBuy
					})
					// uni.navigateBack()
				} else if (this.source == 3) {
					uni.setStorageSync('addre', JSON.stringify(item));
					uni.navigateTo({
						url: '/pages/user/Exchangeorder?id=' + this.id + '&shopid=' + this.shopid + '&zhiboid=' + this.zhiboid
					})

				}else if(this.source ==4){//果树成熟添加收货地址
					uni.setStorageSync('addre', JSON.stringify(item));
					this.$navTo('/pages/additional/GameTrees');
				}
			},
			addAddress(type, item) {
				if (this.type == '1') {
					uni.navigateTo({
						url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&btype=${this.type}&cartids=${this.cartids}&source=${this.source}&zhiboid=${this.zhiboid}`
					})
				} else if (this.type == '2') {
					uni.navigateTo({
						url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&btype=${this.type}&id=${this.id}&optionid=${this.optionid}&total=${this.total}&source=${this.source}&zhiboid=${this.zhiboid}&isVip=${this.isVip}&isGroupBuy=${this.isGroupBuy}`
					})
				} else if (this.type == '3') {
					uni.navigateTo({
						url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&btype=${this.type}&id=${this.id}&shopid=${this.shopid}&source=${this.source}&zhiboid=${this.zhiboid}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&btype=${this.type}&id=${this.id}&optionid=${this.optionid}&total=${this.total}&source=${this.source}&zhiboid=${this.zhiboid}`
					})
				}
			},
			//添加或修改成功之后回调
			// refreshList(data, type){
			// 	//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
			// 	this.addressList.unshift(data);
			// }
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
		background: #FBF8FE;
	}
	.bg{
		width: 100%;
		background: linear-gradient( 180deg, #4B05A7 0%, #A34DE8 52%, #FBF8FE 100%);
		height: 400rpx;
		position: fixed;
		z-index: 1;
	}
	.content {
		/* position: relative; */
	}

	.list {
		position: relative;
		z-index: 2;
		padding: 20rpx 30rpx;
	}
	.item{
		background: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}
	.address-box {
		display: flex;
		align-items: flex-start;
		border-bottom: 1rpx solid #DEDEDE;
		padding: 20rpx 0;
		.tag {
			font-size: 20rpx;
			color: #8615E1;
			margin-right: 14rpx;
			background: #EBD9FA;
			border-radius: 4rpx;
			height: 28rpx;
			line-height: 28rpx;
			padding: 0 10rpx;
			margin-top: 2rpx;
		}

		.address {
			font-size: 24rpx;
			color: #333333;
			flex: 1;
		}
		image{
			width: 40rpx;
		}
	}
	.edit{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-top: 20rpx;
		color: #999999;
		font-size: 24rpx;
		text{
			padding-left: 6rpx;
		}
	}
	.u-box {
		font-size: 28rpx;
		color: #333333;
		font-weight: 600;
		.name {
			margin-right: 14rpx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 60rpx;
		margin: auto;
		z-index: 95;
		width: 460rpx;
		height: 94rpx;
		line-height: 94rpx;
		font-size: 32upx;
		color: #fff;
		font-weight: 600;
		background: linear-gradient( 90deg, #A14FE7 0%, #510AAB 100%);
		border-radius: 66rpx;
		text-align: center;
	}
</style>
